package android.songfinder;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class SongFinderListViewAdapter extends ArrayAdapter<String> {
	  private final Context context;
	  private final ArrayList<String> values;
	  public static SongFinderActivity activity;
	  public static String CurrentSelItem;

	  public SongFinderListViewAdapter(Context context, ArrayList<String> values) {
		    super(context, R.layout.rowlayout, values);
		    this.context = context;
		    this.values = values;
		  }
	  @Override
	  public View getView(int position, View convertView, ViewGroup parent) {
	    LayoutInflater inflater = (LayoutInflater) context
	        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
	    View topView = convertView;
	    final TextView textView = (TextView) rowView.findViewById(R.id.txtFileName);
	    textView.setText(values.get(position));
	    Button btnList = (Button) rowView.findViewById(R.id.btnLists);
	    btnList.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String ss = textView.getText().toString();
				CurrentSelItem = ss;
				Activity act2 = new ListMaintenanceActivity();
				Intent intent = new Intent(v.getContext(), ListMaintenanceActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				//intent.setData();
				v.getContext().startActivity(intent);
				//activity.startActivity(intent);
			}
		});
	    Button btnView = (Button) rowView.findViewById(R.id.btnView);
	    btnView.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String ss = textView.getText().toString();
	          	  Uri data = Uri.parse("file://" + "/storage/Lyrics/" + ss); 
	          	  Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
	                intent.setDataAndType(data,"text/html"); 
	                // /Storage/Lyrics
	                try { 
	                          activity.startActivity(intent); 
	                          activity.SetSearchText("");
	                   } catch (ActivityNotFoundException e) { 
	                          e.printStackTrace(); 
	                   }            	  
			}
		});
	    //ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
	    //textView.setText(values[position]);
	    //// Change the icon for Windows and iPhone
	    //String s = values[position];
	    //if (s.startsWith("iPhone")) {
	    //  imageView.setImageResource(R.drawable.no);
	    //} else {
	    //  imageView.setImageResource(R.drawable.ok);
	    //}

	    return rowView;
	  }
}
